<template>
    <div class="icon-selected">
        <el-input placeholder="请选择图标" :disabled="disabled" @focus="show = true" @blur="show = false" v-model="inputValue" clearable="">
            <el-button slot="append">
                <i :class="inputValue"></i>
            </el-button>
        </el-input>
        <transition name="iconSelectedLayerAnimate">
            <div class="icon-container" v-if="show">
                <div class="icons">
                    <div class="icon" v-for="(icon,i) in icons" :key="i" @click="inputValue = icon.value">
                        <i :class="icon.value"></i>
                    </div>
                </div>
            </div>
        </transition>
    </div>
</template>

<script>
const icons = require("./db.json");

export default {
    name: "icon-selected",
    data() {
        return {
            icons,
            show: false,
            inputValue: "",
        };
    },
    created() {
        this.inputValue = this.value;
    },
    watch: {
        value(value) {
            this.inputValue = value;
        },
        inputValue(inputValue) {
            this.$emit("input", inputValue);

            if (inputValue) {
                const results = [];
                icons.forEach((icon) => {
                    const val1 = icon.value.toLocaleLowerCase();
                    const val2 = inputValue.toLocaleLowerCase();
                    if (val1.indexOf(val2) !== -1) {
                        results.push(icon);
                    }
                });
                this.icons = results;
            } else {
                this.icons = icons;
            }
        },
    },
    props: {
        value: {
            type: String,
            default() {
                return "";
            },
        },
        disabled: {
            type: Boolean,
            default() {
                return false;
            },
        },
    },
};
</script>

<style lang="scss" scoped>
.icon-selected {
    position: relative;
    z-index: 9;
    .icon-container {
        top: 42px;
        position: absolute;
        z-index: 1;
        left: 0;
        width: 400px;
        height: 300px;
        border-radius: 3px;
        box-shadow: 0px 1px 5px rgba($color: #000000, $alpha: 0.1);
        overflow-y: auto;
        background-color: #fff;
        .icons {
            margin: 0px;
            padding: 0px;
            display: flex;
            flex-wrap: wrap;
            .icon {
                display: inline-flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                width: 64px;
                height: 64px;
                cursor: pointer;

                > i {
                    font-size: 24px;
                    transition: 0.2s;
                }

                &:hover {
                    > i {
                        transform: scale(1.2);
                        color: #409eff;
                    }
                }
            }
        }
    }
}

.iconSelectedLayerAnimate-enter-active {
    animation: iconSelectedLayerAnimate 0.2s linear;
}

.iconSelectedLayerAnimate-leave-active {
    animation: iconSelectedLayerAnimate 0.2s linear reverse;
}

@keyframes iconSelectedLayerAnimate {
    0% {
        opacity: 0;
        transform: translateY(5px);
    }
    100% {
        opacity: 1;
        transform: translateY(0px);
    }
}
</style>